<template>
  <div class="myExam-container">
    <div style="width:13%;float:left;" class="background">
      <el-input placeholder="输入关键字进行过滤" 
      suffix-icon="el-icon-search" 
      v-model="filterText">
      </el-input>
      <el-container class="leftcont">
        <el-aside width="100%">
          <el-tree 
          :data="items" 
          :props="defaultProps" 
          @node-click="handleNodeClick" 
          v-loading="treeLoading"
          ref="tree"
          :filter-node-method="filterNode"
          :render-content="renderContent"
          ></el-tree>
        </el-aside>
      </el-container>
      <!-- <el-dialog title="添加行业分类" :visible.sync="dialogFormVisible">
          <el-form :model="typeForm" :rules="typeRules" ref="typeForm">
              <el-form-item label="行业分类名称" label-width="">
              <el-input v-model="typeForm.typeName" auto-complete="off"></el-input>
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="add()" :loading="loadingBtn">确 定</el-button>
          </div>
        </el-dialog> -->
    </div>
    <el-container class="zindex">
      <el-main>
        <el-form>
          <el-form-item label="关键词" style="display: inline-block; width: 20%; margin-left: 0px;" class="question-types">
            <el-col :span="5" class="tag">
              <el-form-item prop="">
                <el-input  v-model="keyWord"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-button type="primary" class="search" @click="search()" :loading="btnLoading">查询</el-button>
          <div class="selectbtn">
            <el-button type="primary" class="batch" @click="selectDel()" :loading="btnLoading">批量删除</el-button>
            <el-button type="primary" class="batch" @click="goForm" :loading="btnLoading">创建试卷</el-button>
          </div>
        </el-form>
        <el-table 
        :data="tableData" style="width: 100%" 
        v-loading="tableLoading"
        @selection-change="handleSelectionChange">
          <el-table-column prop="id" type="selection" width="54"></el-table-column>
          <el-table-column type="index" label="序号" width="54"></el-table-column>
          <el-table-column label="试卷名称" width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.upVo.title }}</span>
            </template>
          </el-table-column>
          <el-table-column label="名字" width="88">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.upVo.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="题量" width="88">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.questionTotal }}</span>
            </template>
          </el-table-column>
          <el-table-column label="总回收/总发送" width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px; padding:0 10px" class="cursor">{{ scope.row.sumitCount }}</span>&nbsp;/
              <span style="margin-left: 2px; padding:0 10px" class="cursor">{{ scope.row.allCount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="最后修改时间" width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.updateDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="mini" type="primary"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next" :total="pageTotal">
          </el-pagination>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import { isvalidateRequired } from '@/utils/validate'
import { fetchTree } from '@/api/profession'
import { fetchList, fetchDel, fetchListSearch, fetchAllList } from '@/api/quiz'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      treeLoading: false,
      filterText: '',
      items: [],
      keyWord: '',
      tableData: [],
      tableLoading: false,
      pageSize: 10,
      pageTotal: 100,
      currentPage: 1,
      multipleSelection: [],
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.getExamList()
    this.allList()
  },
  methods: {
    allList() {
      this.tableLoading = true
      fetchAllList().then(response => {
        // console.log(response)
        this.tableData = response.data.data
        this.tableLoading = false
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    renderContent(h, { node, data, store }) {
      return (
        <div style='display:inline-block;text-algin:right;'>
          <span style='flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;'>
            <span>
              <span on-click={() => this.getQuestionListByType(data.value)} id='nodeLabel' style='text-align: left;'>
                {node.label}
              </span>
            </span>
            <span style=''>
              <el-button style='font-size: 12px;' type='text' on-click={() => this.append(data)}>添加</el-button>
              <el-button style='font-size: 12px;' type='text' on-click={() => this.remove(node, data)}>删除</el-button>
            </span>
          </span>
        </div>
      )
    },
    getList() {
      this.treeLoading = true
      fetchTree().then(response => {
        this.items = response.data.data
        this.treeLoading = false
      })
    },
    getExamList() {
      this.tableLoading = true
      fetchList(this.currentPage, this.pageSize).then(response => {
        this.pageTotal = response.data.total
        this.tableData = response.data.data
        this.tableLoading = false
      })
    },
    search() {
      this.currentPage = 1
      fetchListSearch(this.keyWord).then(response => {
        this.pageTotal = response.data.total
        this.tableData = response.data.data
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getExamList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getExamList()
    },
    handleDelete(index, row) {
      this.$confirm('删除后不可恢复,确认删除？').then(response => {
        fetchDel(row.id).then(response => {
          Message.success('删除试卷成功')
          this.getExamList()
        })
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },
    selectDel() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('删除后不可恢复,确认删除？').then(response => {
          var ids = []
          this.multipleSelection.forEach(function(item) {
            ids.push(item.id)
          })
          fetchDel(ids).then(response => {
            Message.success('删除试卷成功')
            this.getExamList()
          })
        })
      } else {
        Message.warning('请选择需要删除的试卷')
      }
    },
    goForm() {
      this.$router.push({
        path: '/form',
        name: 'quiz-form'
      })
    },
    goTrated(){
      this.$router.push({
        path: '/treated',
        name: 'quiz-treated'
      })
    }
  }
}
</script>
<style scoped>
.myExam-container {
  margin: 30px;
  margin-left: 0;
}
.leftcont {
  width: 100%;
}
.background {
  height: 100%;
  text-align: center;
  position: absolute;
  left: 180px;
  right: 0;
  top: 0;
  bottom: 0;
  border-right: 1px solid #000;
  padding-top: 104px;
}
.zindex,.background {
  z-index: -1;
}
.background .el-input {
  width: 80%;
}

.background>>>.el-input__inner {
  border-radius: 18px;
}

.el-aside {
  color: #333;
  text-align: center;
  overflow: hidden;  
  width: 300px!important;
}

.el-aside > .el-tree {
  border: 1px solid #d1dbe5;
  margin-top: 20px;
}

.el-aside>>>.el-tree-node {
  background-color: #ffdca9;
}

.app-container {
  padding-top: 0;
}

.el-main {
  color: #333;
  text-align: left;  
  border-bottom: 0;
  padding: 0 20px; 
  margin-left: 250px; 
}
.cursor:hover{
  cursor: pointer; 
}
.filter-container {
  padding-bottom: 4px;
}

.el-form {
  margin-left: 10px;
  padding-right: 14%;
}

.question-types>>>.el-form-item__label {
  width: 60px !important;
}

.question-types>>>.el-form-item__content {
  margin-left: 0px !important;
}

.search {
  vertical-align: top;
  margin-left: -20px;
}

.tag {
  width: 60%;
}

.selectbtn {
  display: inline-block;
  float: right;
}
.page {
  text-align: center;
  margin-top: 30px;
}
.el-pagination {
  display: inline-block;
  margin: 0 auto;
  height: 28px;
  line-height: 28px;
  padding: 0;
}
</style>